/*
 * Copyright (C) 2011 Nicolas BARBOTIN
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include "NMI.h"

SDL_Surface *lireImage(const char *fichier)
{
    FILE *fic = NULL;
    fic = fopen(fichier, "r");

    if(fic == NULL) {
        std::cout << "Fichier introuvable" << std::endl;
        return NULL;
    }

    int hauteur = 0, largeur = 0;
    fscanf(fic, "%d %d", &largeur, &hauteur);
    SDL_Surface *img = SDL_CreateRGBSurface(SDL_HWSURFACE, largeur, hauteur, 32, 0, 0, 0, 0);

    int posX = 0, posY = 0;
    while(fgetc(fic) != '.') {
        int rouge, vert, bleu;
        fscanf(fic, "%d,%d,%d", &rouge, &vert, &bleu);

        SDL_Rect pos;
        pos.x = posX;
        pos.y = posY;
        pos.h = 1;
        pos.w = 1;
        SDL_FillRect(img, &pos, SDL_MapRGB(img->format, rouge, vert, bleu));

        if(posX == largeur - 1) {
            posX = 0;
            posY++;
        } else {
            posX++;
        }
    }

    fclose(fic);
    return img;
}

SDL_Surface *tournerSurface(SDL_Surface *surface) {
    SDL_Surface *ret = SDL_CreateRGBSurface(surface->flags, surface->h, surface->w, 32, surface->format->Rmask, surface->format->Gmask, surface->format->Bmask, surface->format->Amask);

    int x = 0, y = 0;
    Uint8 *pixel;

    while(x < surface->w && y < surface->h) {
        pixel = (Uint8*) surface->pixels + y * surface->pitch + x * surface->format->BytesPerPixel;

        SDL_Rect pos;
        pos.x = (ret->w - 1) - y;
        pos.y = x;
        pos.w = 1;
        pos.h = 1;

        SDL_FillRect(ret, &pos, SDL_MapRGB(surface->format, pixel[2], pixel[1], pixel[0]));

        if(x == surface->w - 1) {
            x = 0;
            y++;
        } else {
            x++;
        }
    }

    return ret;
}

SDL_Surface *retournerSurface(SDL_Surface *surface, NMI_ANGLE angle) {
    switch(angle) {
        case NMIA_90:
            return tournerSurface(surface);
            break;

        case NMIA_180:
            return tournerSurface(tournerSurface(surface));
            break;

        case NMIA_270:
            return tournerSurface(tournerSurface(tournerSurface(surface)));
            break;
    }
}

void convertirImage(const char *source, const char *destination)
{
    convertirSurface(SDL_LoadBMP(source), destination);
}

void convertirSurface(SDL_Surface *src, const char *destination) {
    FILE *fichier = fopen(destination, "w");

    fprintf(fichier, "%d %d ", src->w, src->h);

    int posX = 0, posY = 0, max = src->w * src->h;
    Uint8 *pixel;

    for(int i = 0; i < max; i++) {
        pixel = (Uint8*) src->pixels + posY * src->pitch + posX * src->format->BytesPerPixel;
        if(i == max - 1) {
            fprintf(fichier, "%d,%d,%d.", pixel[2], pixel[1], pixel[0]);
        } else {
            fprintf(fichier, "%d,%d,%d ", pixel[2], pixel[1], pixel[0]);
        }

        if(posX == src->w - 1) {
            posX = 0;
            posY++;
        } else {
            posX++;
        }
    }

    SDL_FreeSurface(src);
    fclose(fichier);
}

SDL_Surface **lireMap(const char *fichier, int taille, Uint8 r, Uint8 v, Uint8 b) {
    SDL_Surface *fic = lireImage(fichier);
    const int max = fic->w / taille;
    SDL_Surface **tiles = (SDL_Surface**) malloc(sizeof(SDL_Surface*) * max);
    SDL_Rect pos;
    pos.y = 0;
    pos.w = taille;
    pos.h = taille;

    SDL_Rect pos2;
    pos2.x = 0;
    pos2.y = 0;

    for(int i = 0; i < max; i++) {
        pos.x = i * taille;

        tiles[i] = SDL_CreateRGBSurface(SDL_HWSURFACE, taille, taille, 32, r, v, b, 0);
        SDL_BlitSurface(fic, &pos, tiles[i], &pos2);
    }

    return tiles;
}
